﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using PNProject.Core;

namespace PNProject.EF
{
    public class LibraryRepo : IRepository<Library>
    {
        public IEnumerable<Library> GetAll()
        {
            using (var context = new PNDatabaseEntities())
            {
                context.Connection.Open();
                context.Libraries.MergeOption = MergeOption.NoTracking;
                var d = context.Libraries;
                return context.Libraries.ToList();
            }
        }

        public IEnumerable<Library> GetAll(string sortField)
        {
            using (var context = new PNDatabaseEntities())
            {
                context.Libraries.MergeOption = MergeOption.NoTracking;

                return context.Libraries.SortBy(sortField).ToList();
            }
        }

        public IEnumerable<Library> GetSome(System.Linq.Expressions.Expression<Func<Library, bool>> predicate)
        {
            using (var context = new PNDatabaseEntities())
            {
                context.Libraries.MergeOption = MergeOption.NoTracking;

                return context.Libraries.Where(predicate).ToList();
            }
        }

        public IEnumerable<Library> GetSome(System.Linq.Expressions.Expression<Func<Library, bool>> predicate, int top, int skip)
        {
            using (var context = new PNDatabaseEntities())
            {
                context.Libraries.MergeOption = MergeOption.NoTracking;

                return context.Libraries.Where(predicate).ToList();
            }
        }

        public Library GetOne(System.Linq.Expressions.Expression<Func<Library, bool>> predicate)
        {
            using (var context = new PNDatabaseEntities())
            {
                return context.Libraries.FirstOrDefault(predicate);
            }
        }

        public void Save(Library original, Library modified)
        {
            using (var context = new PNDatabaseEntities())
            {
                //modified.ModifiedDate = DateTime.UtcNow;

                if (original == null)
                    context.AddToLibraries(modified);
                else
                {
                    context.Attach(original);
                    context.ApplyCurrentValues("Libraries", modified);
                }

                context.SaveChanges();
            }
        }

        public void Delete(Library original)
        {
            using (var context = new PNDatabaseEntities())
            {
                if (original.EntityKey == null)
                {
                    var existing = GetOne(d => d.ElementID == original.ElementID);
                    if (existing != null)
                    {
                        original.EntityKey = existing.EntityKey;
                    }
                }

                context.Attach(original);
                context.DeleteObject(original);

                context.SaveChanges();
            }
        }
    }
}